//
//  ViewController.m
//  JsonTest
//
//  Created by FLY on 15/5/26.
//  Copyright (c) 2015年 Light. All rights reserved.
//

#import "ViewController.h"

#define LightURL @"http://123.57.221.116:8080/light-server/intf/user/login.shtml"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSDictionary *userDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"123456",@"pwd",@"13312345678",@"code", nil];
    NSError * error;
    NSLog(@"userDictionary is %@",userDictionary);
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userDictionary options:NSJSONWritingPrettyPrinted error:&error];
    NSString *user = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"userString is %@",user);
    NSLog(@"userJson is %@",jsonData);
    NSLog(error);
    NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];

    
    NSURL *url = [NSURL URLWithString:LightURL];
    
    NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:url];
    [requst setHTTPMethod:@"POST"];
    [requst setHTTPBody:tempJsonData];
    
//    NSLog(@"jsonData is %@",tempJsonData);
    
    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    [NSURLConnection sendAsynchronousRequest:requst queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(connectionError);
        if(data){
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSString *error1 = dict[@"error"];
            if(error1){
                NSLog(error1);
            }
            else
            {
                NSString *success = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(success);
            }
        }else{
            NSLog(@"connection error");
        }
    }];
    NSLog(@"finish!");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
